package com.byx.chatclient.presenter;

import com.byx.chatclient.im.AbstractMessageSender;
import com.byx.chatclient.view.IBaseView;
import com.byx.chatclient.view.activity.BaseActivity;

import java.lang.ref.WeakReference;

/**
 * BasePresenter
 *
 * @author Superb
 * @date 2021/2/22 10:14
 */
public abstract class BasePresenter<V extends IBaseView> implements Presenter<V>{
    private WeakReference<V> viewReference;
    protected V view;

    @Override
    public void attachView(V view) {
        viewReference=new WeakReference<>(view);
    }

    @Override
    public void detachView() {
        if(viewReference != null){
            viewReference.clear();
            viewReference = null;
        }
    }

    @Override
    public boolean isActive() {
        if(viewReference != null){
            view = viewReference.get();
        }
        return view != null;
    }

    @Override
    public String getName() {
        if(isActive()){
            return view.getClass().getSimpleName();
        }
        return null;
    }
}
